﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	public class HYGFSystemHideShow : HYNoTranExternalCommand
	{
		public override void FilterProcess()
		{
			base.FilterViewType.Add(4);
			base.FilterProcess();
		}

		public override Result HYExecute()
		{
			FrmSystemShowHide frmSystemShowHide = new FrmSystemShowHide(base.UiDoc, "消防排水");
			if (frmSystemShowHide.GetMepSystemsBySysName("消防排水") == null)
			{
				YJKMessageBox.Show("当前项目内没有搜到系统！");
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			DialogResult dialogResult = DialogResult.OK;
			while (dialogResult == DialogResult.OK)
			{
				dialogResult = frmSystemShowHide.ShowDialog();
				if (dialogResult == DialogResult.Retry)
				{
					try
					{
						string specialSysName = frmSystemShowHide.specialSysName;
						IList<Element> listElement = base.Sel.PickElementsByRectangle("请框选要加入 " + specialSysName + " 系统内的实体……");
						frmSystemShowHide.RecordElementToSystem(specialSysName, listElement);
						dialogResult = DialogResult.OK;
					}
					catch (Autodesk.Revit.Exceptions.OperationCanceledException)
					{
						dialogResult = DialogResult.OK;
					}
					catch (Exception ex)
					{
						YJKMessageBox.Show(ex.Message);
						dialogResult = DialogResult.OK;
					}
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public override void TailProcess()
		{
			if (this.tran != null)
			{
				TransactionStatus status = this.tran.GetStatus();
				if (status == 1)
				{
					this.tran.RollBack();
					return;
				}
				if (status == 5)
				{
					this.tran.RollBack();
				}
			}
		}

		private Transaction tran;
	}
}
